﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using DC.BLL;

namespace DC.BLL
{
    public partial class List : DtCms.Web.UI.ManagePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            //NewMethod(); return;
            //initSeller();return;
            #endregion
            if (!this.IsPostBack)
            {
                List<CouponItemCard> list = XmlProvider.GetAllItemCard();
                rptList.DataSource = list;
                rptList.DataBind();

                List<CardSeller> seller = XmlProvider.GetAllCardSeller();

                foreach (CardSeller cs in seller)
                {
                    this.CardSellerList.Items.Add(new ListItem(cs.SellerNamer, cs.SellerId.ToString()));
                }
            }
            if(!string.IsNullOrEmpty(Request.Params["op"]) && !string.IsNullOrEmpty(Request.Params["id"]))
            {
                List<CouponItemCard> list = XmlProvider.GetAllItemCard();
                Guid id=new Guid(Request.Params["id"]);
                CouponItemCard card=list.Where(o => o.CardId.Equals(id)).FirstOrDefault();
                if (list.Contains(card))
                {
                    list.Remove(card);
                    XmlProvider.SaveAllItemCard();
                    JscriptPrint("Delete The Row Success ！", "#", "success");
                    FilterCard();
                }
            }
        }

        private static void NewMethod()
        {
            List<CouponItemCard> cards = new List<CouponItemCard>();
            cards.Add(new CouponItemCard()
            {
                CardName = "KFC优惠券1",
                CardUrl = "http://nav.58control.cn/uploads/image/KFC-1.jpg",
                ExpiredDate = DateTime.Today.AddMonths(1),
                Price = 15.00F,
                Seller = 0
            });
            cards.Add(new CouponItemCard()
            {
                CardName = "KFC优惠券2",
                CardUrl = "http://nav.58control.cn/uploads/image/KFC-1.jpg",
                ExpiredDate = DateTime.Today.AddMonths(1),
                Price = 15.00F,
                Seller = 0
            });
            cards.Add(new CouponItemCard()
            {
                CardName = "KFC优惠券3",
                CardUrl = "http://nav.58control.cn/uploads/image/KFC-1.jpg",
                ExpiredDate = DateTime.Today.AddMonths(1),
                Price = 15.00F,
                Seller = 0
            });
            cards.Add(new CouponItemCard()
            {
                CardName = "KFC优惠券4",
                CardUrl = "http://nav.58control.cn/uploads/image/KFC-1.jpg",
                ExpiredDate = DateTime.Today.AddMonths(1),
                Price = 15.00F,
                Seller = 0
            });
            cards.Add(new CouponItemCard()
            {
                CardName = "KFC优惠券5",
                CardUrl = "http://nav.58control.cn/uploads/image/KFC-1.jpg",
                ExpiredDate = DateTime.Today.AddMonths(1),
                Price = 15.00F,
                Seller = 0
            });

            XmlProvider.ItemCardList = cards;
            XmlProvider.SaveAllItemCard();
        }


        public static void initSeller()
        {
            List<CardSeller> sellers = new List<CardSeller>();
            sellers.Add(new CardSeller()
            {
                SellerId = 0,
                SellerNamer = "KFC",
                LogoUrl = "http://www.dicomerxi.com.ec/userfiles/kfc.png"
            });
            sellers.Add(new CardSeller()
            {
                SellerId = 1,
                SellerNamer = "KFC2",
                LogoUrl = "http://www.dicomerxi.com.ec/userfiles/kfc.png"
            });
            sellers.Add(new CardSeller()
            {
                SellerId = 2,
                SellerNamer = "KFC2",
                LogoUrl = "http://www.dicomerxi.com.ec/userfiles/kfc.png"
            });
            sellers.Add(new CardSeller()
            {
                SellerId = 3,
                SellerNamer = "KFC2",
                LogoUrl = "http://www.dicomerxi.com.ec/userfiles/kfc.png"
            });
            XmlProvider.CardSelllerList = sellers;
            XmlProvider.SaveAllCardSeller();
        }


        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterCard();
        }

        protected void Seller_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterCard();
        }

        private void FilterCard()
        {
            List<CouponItemCard> list = XmlProvider.GetAllItemCard();
            //未过期
            if (ddlProperty.SelectedIndex == 1)
            {
                rptList.DataSource = list.Where(o => (o.ExpiredDate >= DateTime.Today)
                                                    && (this.CardSellerList.SelectedIndex == 0 ? true : o.Seller == Int32.Parse(this.CardSellerList.SelectedValue)));
            }
            else if (ddlProperty.SelectedIndex == 2)
            {
                rptList.DataSource = list.Where(o => (o.ExpiredDate < DateTime.Today)
                                                    && (this.CardSellerList.SelectedIndex == 0 ? true : o.Seller == Int32.Parse(this.CardSellerList.SelectedValue)));
            }
            else
                rptList.DataSource = list.Where(o =>(this.CardSellerList.SelectedIndex == 0 ? true : o.Seller == Int32.Parse(this.CardSellerList.SelectedValue))); ; 
                                                    
            rptList.DataBind();
        }
    }
}
